<script setup lang="ts">
import { createConsultOrder, getConsultOrderPre } from '@/services/consult';
import { getPatientDetail } from '@/services/user';
import { useConsultStore } from '@/stores';
import type { ConsultOrderPreData, PartialConsult } from '@/types/consult';
import type { Patient } from '@/types/user';
import { showConfirmDialog, showDialog,  showSuccessToast } from 'vant';
import { onMounted, ref } from 'vue';
import { onBeforeRouteLeave, useRouter } from 'vue-router';

// 获取预支付信息
const payInfo = ref<ConsultOrderPreData>()

const store = useConsultStore()

const loadData = async()=>{
  const res = await getConsultOrderPre({
    type:store.consult.type,
    illnessType:store.consult.illnessType
  })
  payInfo.value = res.data
  // 记录优惠券的id
  store.setCoupon(res.data.couponId)
}
// 患者详情
const patient = ref<Patient>()
const loadPatient = async()=>{
  if(!store.consult.patientId) return
  const res = await getPatientDetail(store.consult.patientId)
  patient.value = res.data
}
type key =keyof PartialConsult
onMounted(()=>{
  // 生产订单需要的信息不完整的时候需要提示
  const validKeys:key[] = ['type', 'illnessType', 'illnessDesc', 'patientId','depId','illnessTime','consultFlag']
  const valid = validKeys.every(key => store.consult[key] !== undefined)
  if(!valid) {
    return showDialog({
      title: '温馨提示',
      message: '问诊信息不完整请重新填写，如有未支付的问诊订单可在问诊记录中继续支付',
      closeOnPopstate:false
    }).then(()=>{
      // 跳转到首页
      router.push('/')
    })
  }
  loadData()
  loadPatient()
})
// 同意协议
const agree = ref(false)

// 复制支付账号
const onCopy = (id:string)=>{
  // 复制身份证号
  navigator.clipboard.writeText(id).then(()=>{
    showSuccessToast('复制成功')
  }).catch(()=>{
    showSuccessToast('复制失败，请手动复制')
  })
}

// 支付抽屉
const show = ref(false)
const loading = ref(false)
const orderId = ref()
const submit = async()=>{
  if(!agree.value) {
    showDialog({
      title: '请同意支付协议'
    })
    return
  }
  loading.value = true
  show.value = true
  // 发送支付请求
  const res =await createConsultOrder(store.consult)
  store.clear() // 清理数据
  orderId.value = res.data.id
  loading.value = false
}

// 提示1：取消支付无法获得医生回复，医生接诊名额有限，是否确认关闭？
// 提示2: 问诊信息不完整请重新填写，如有未支付的问诊订单可在问诊记录中继续支付

// 用户引导
onBeforeRouteLeave(()=>{
  if(orderId.value) return false // 如果有订单id则不提示
})
const router = useRouter()
const onClose = ()=>{
  return showConfirmDialog({
    title:'温馨提示',
    message:'取消支付无法获得医生回复，医生接诊名额有限，是否确认关闭？',
    cancelButtonText:'狠心离开',
    confirmButtonText:'继续支付',
// 提示2: 问诊信息不完整请重新填写，如有未支付的问诊订单可在问诊记录中继续支'
  }).then(()=>{
    return false
  }).catch(()=>{
    orderId.value = ''
    router.push('/user/consult')
    return true
  })
}

</script>

<template>
  <div class="consult-pay-page" v-if="payInfo && patient">
    <cp-nav-bar title="支付" />
    <div class="pay-info">
      <p class="tit">图文问诊 {{ payInfo.payment }} 元</p>
      <img class="img" src="@/assets/avatar-doctor.svg" />
      <p class="desc">
        <span>极速问诊</span>
        <span>自动分配医生</span>
      </p>
    </div>
    <van-cell-group>
      <van-cell title="优惠券" :value="`-¥${payInfo?.couponDeduction}`" />
      <van-cell title="积分抵扣" :value="`-¥${payInfo?.pointDeduction}`" />
      <van-cell title="实付款" :value="`-¥${payInfo?.actualPayment}`" class="pay-price" />
    </van-cell-group>
    <div class="pay-space"></div>
    <van-cell-group>
      <van-cell title="患者信息" :value="`${patient.name} | ${patient.genderValue} | ${patient.age}岁`"></van-cell>
      <van-cell title="病情描述" :label="store.consult.illnessDesc"></van-cell>
    </van-cell-group>
    <div class="pay-schema">
      <van-checkbox v-model="agree">我已同意 <span class="text">支付协议</span></van-checkbox>
    </div>
    <!-- 测试账号 -->
     <div class="demo-pay">
      <van-cell>测试账号</van-cell>
      <div class="demo-content">
        <span>测试账号：</span>
        <span>scobys4865@sandbox.com</span>
        <van-button @click="onCopy('scobys4865@sandbox.com')" style="font-size: 12px;height: 25px;" type="primary">复制</van-button>
      </div>
      <div class="demo-content">
        <span>测试账号：</span>
        <span>askgxl8276@sandbox.com</span>
        <van-button @click="onCopy('askgxl8276@sandbox.com')" style="font-size: 12px;height: 25px;" type="primary">复制</van-button>
      </div>
      <div class="demo-content">
        <span>登录密码：</span>
        <span>111111</span>
        <van-button @click="onCopy('111111')" style="font-size: 12px;height: 25px;" type="primary">复制</van-button>
      </div>
      <div class="demo-content">
        <span>支付密码：</span>
        <span>111111</span>
        <van-button @click="onCopy('111111')" style="font-size: 12px;height: 25px;" type="primary">复制</van-button>
      </div>
     </div>
    <van-submit-bar
     :loading="loading"
    button-type="primary" :price="payInfo.actualPayment * 100" button-text="立即支付" text-align="left" @click="submit" />
    <!-- 支付抽屉 -->
     <cp-pay-sheet v-model:show="show" :order-id="orderId" pay-callback="/room" :actual-payment="payInfo.actualPayment" @on-close="onClose"></cp-pay-sheet>
  </div>
  <div class="consult-pay-page" v-else>
    <cp-nav-bar title="支付"></cp-nav-bar>
    <van-skeleton title style="margin-top: 18px;" row="10"></van-skeleton>
  </div>
</template>

<style lang="scss" scoped>
.consult-pay-page {
  padding: 46px 0 50px 0;
}
.pay-info {
  display: flex;
  padding: 15px;
  flex-wrap: wrap;
  align-items: center;
  .tit {
    width: 100%;
    font-size: 16px;
    margin-bottom: 10px;
  }
  .img {
    margin-right: 10px;
    width: 38px;
    height: 38px;
    border-radius: 4px;
    overflow: hidden;
  }
  .desc {
    flex: 1;
    > span {
      display: block;
      color: var(--cp-tag);
      &:first-child {
        font-size: 16px;
        color: var(--cp-text2);
      }
    }
  }
}
.pay-price {
  ::v-deep() {
    .vam-cell__title {
      font-size: 16px;
    }
    .van-cell__value {
      font-size: 16px;
      color: var(--cp-price);
    }
  }
}
.pay-space {
  height: 12px;
  background-color: var(--cp-bg);
}
.pay-schema {
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: center;
  .text {
    color: var(--cp-primary);
  }
}
::v-deep() {
  .van-submit-bar__button {
    font-weight: normal;
    width: 160px;
  }
}

</style>
